import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Map;

import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class RedSalamanderCatcher extends Script implements
		ServerMessageListener, PaintListener { // Names the script

	boolean Pickup = false;
	boolean AntiBanDebug = true;
	int avgPerHour;
	long avoidedCombat;
	long avoidedSmokingRocks;
	int Caught = 19675;

	int checkTime;

	int countToNext;

	boolean Debug = false;

	int FailCount = 0;

	long lastAvgCheck;

	long lastCheck;

	int Lizard = 10149;
	int Lizards = 0;
	int Net = 303;
	int NormalTree = 19679;
	int oldExp;
	int oldLevels;
	int oldRockCount; // /////////CREDS TO AELIN
	int Points = 0;
	String PointsString = "LuLWuT?";
	boolean powerMode = false;
	int randomRun = random(40, 75);
	int rockCount;
	int Rope = 954;
	int startExp = 0;
	public int startLevel = 0;
	int StartPoints = 0;
	public long startTime = System.currentTimeMillis();
	public int startXP = 0;
	public long time = System.currentTimeMillis();
	int Trapped = 19678;
	int xpPerRock;

	private final RSTile HunterTile = new RSTile(2448, 3226);

	// CREDITS TO Nvv FOR THE TILES CASTEL WARS - HUNTER SPOT!

	RSTile[] HunterPath = { new RSTile(2442, 3090), new RSTile(2456, 3100),
			new RSTile(2450, 3116), new RSTile(2437, 3126),
			new RSTile(2433, 3138), new RSTile(2448, 3147),
			new RSTile(2454, 3162), new RSTile(2466, 3172),
			new RSTile(2473, 3187), new RSTile(2474, 3204),
			new RSTile(2476, 3217), new RSTile(2478, 3233),
			new RSTile(2474, 3238) };

	public static final int[] ReleaseSalamander = new int[] { 10147 };

	public boolean atTileTake(final RSTile tile, final String action) {
		int TempCounter = 0;
		while (true) {
			if (!getMyPlayer().isMoving()) {
				break;
			}
			wait(random(200, 400));
		}
		try {
			Point location = Calculations.tileToScreen(tile);
			if (location.x == -1 || location.y == -1) {
				return false;
			}
			moveMouse(location, 8, 8);
			wait(random(50, 100));
			while (!getMenuItems().get(0).toLowerCase().contains(
					action.toLowerCase())
					&& TempCounter < 5) {
				location = Calculations.tileToScreen(tile);
				moveMouse(location, 8, 8);
				TempCounter++;
			}
			if (getMenuItems().get(0).toLowerCase().contains(
					action.toLowerCase())) {
				clickMouse(true);
			} else {
				clickMouse(false);
				atMenu(action);
			}
			wait(random(50, 100));
			return true;
		} catch (final Exception e) {
			return false;
		}
	}

	@Override
	public String getAuthor() { // Sets the Authors name
		return "Hellfish+TimK+Drizzt";
	}

	@Override
	public String getName() { // Sets the Scripts name
		return "Red Salamander Catcher";
	}

	@Override
	public String getScriptCategory() { // Sets the category that the script
		// fit's into
		return "Hunter"; // ex Woodcutting, Fishing, Money Making, Quests
	}

	@Override
	public String getScriptDescription() { // Shows a description of the script
		// inside the script dump area
		String html = "<html>";
		html += "<body>";
		html += "Description";
		html += "</body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() { // Sets the Version number of the script
		return 1.1;
	}

	@Override
	public int loop() { // This is the area of the script where you code out the
		// things you want the bot to do

		// setCameraRotation(181);
		setCameraRotation(359);

		if (inventoryContains(10147)) {
			atInventoryItem(10147, "Release");
		}

		final RSItemTile rope = getGroundItemByID(Rope);
		if (rope != null && !isInventoryFull()) {
			atTileTake(rope, "Take");
			wait(random(200, 900));
			return random(200, 400);
		}
		final RSItemTile net = getGroundItemByID(Net);
		if (net != null && !isInventoryFull()) {
			atTileTake(net, "Take");
			wait(random(200, 900));
			return random(200, 400);
		}

		if (distanceTo(HunterTile) >= 8) {
			walkPathMM(randomizePath(HunterPath, 2, 2), 20);
			wait(random(2000, 3000));
		}

		if (getMyPlayer().getAnimation() == 5215) {
			moveMouseAntiBan();
			return random(1000, 2000);
		}

		if (getMyPlayer().isMoving()) {
			moveMouseAntiBan();
			return random(150, 200);

		}

		if (isInventoryFull()) {

			Release();

		}

		final RSObject CoughtSalamander = findObject(19659);

		if (CoughtSalamander != null
				&& distanceTo(CoughtSalamander.getLocation()) <= 6) {

			wait(random(500, 1000));

			final Point Sallie1 = new Point(Calculations
					.tileToScreen(CoughtSalamander.getLocation()));
			final Point Sallie2 = new Point(Sallie1.x + random(-0, 0),
					Sallie1.y + random(-0, 0));
			moveMouse(Sallie2, random(-8, 12), random(-5, -5));
			clickMouse(true);
			moveMouseAntiBan();
			wait(random(2000, 3000));

		}
		final RSObject YoungTree = findObject(19663);

		if (YoungTree != null && distanceTo(YoungTree.getLocation()) <= 6) {

			final Point Young1 = new Point(Calculations.tileToScreen(YoungTree
					.getLocation()));
			final Point Young2 = new Point(Young1.x + random(-0, 0), Young1.y
					+ random(-0, 0));
			moveMouse(Young2, random(-3, 14), random(-2, -17));
			clickMouse(true);
			moveMouseAntiBan();
			wait(random(2000, 3000));

		}

		return random(100, 150);
	}

	public void moveMouseAntiBan() {
		final int x = random(0, 750);
		final int y = random(0, 500);
		moveMouse(49, 71, x, y);
	}

	@Override
	public void onFinish() { // Loads any commands to do when the script is
		// ended
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {

			// Ty to AELIN for these
			// local objects:
			final int index = 21;
			int exp = 0;
			int levels;
			long hours = 0, minutes = 0, seconds = 0;
			long time;

			// Has start time been previously set?
			if (startTime == 0) {
				startTime = System.currentTimeMillis();
			}

			// Calculate hours, minutes and seconds.
			time = System.currentTimeMillis() - startTime;
			seconds = time / 1000;
			if (seconds >= 60) {
				minutes = seconds / 60;
				seconds -= minutes * 60;
			}
			if (minutes >= 60) {
				hours = minutes / 60;
				minutes -= hours * 60;
			}

			// Has variables been previously defined?
			if (startLevel == 0 || startExp == 0) {
				// No, so define them now.
				startLevel = skills.getCurrentSkillLevel(21);
				startExp = skills.getCurrentSkillExp(21);
				oldExp = 0;
			}

			// Calculate catches based on experience changes.
			exp = skills.getCurrentSkillExp(21) - startExp;
			if (exp > oldExp) {
				xpPerRock = exp - oldExp;
				oldExp = exp;
				rockCount++;
				countToNext = skills.getXPToNextLevel(Constants.STAT_HUNTER)
						/ xpPerRock + 1;
			}

			// Calculate levels gained.
			levels = skills.getCurrentSkillLevel(index) - startLevel;
			if (levels > oldLevels) {
				oldLevels = levels;
			}

			//
			if (System.currentTimeMillis() - lastAvgCheck >= 60000) {
				lastAvgCheck = System.currentTimeMillis();
				avgPerHour = (rockCount - oldRockCount) * 60;
				oldRockCount = rockCount;
			}

			final Color BG = new Color(0, 0, 0, 75);
			final Color RED = new Color(255, 0, 0, 255);
			final Color GREEN = new Color(0, 255, 0, 255);
			final Color BLACK = new Color(0, 0, 0, 255);

			final int type = 21;
			long millis = System.currentTimeMillis() - startTime;
			millis -= hours * 1000 * 60 * 60;
			millis -= minutes * 1000 * 60;
			final int LevelChange = skills.getCurrentSkillLevel(type)
					- startLevel;
			final int perNextLvl = skills.getPercentToNextLevel(type);
			final int nextLvl = skills.getCurrentSkillLevel(type) + 1;
			g.setColor(BG);
			g.fill3DRect(5, 25, 205, 143, true);
			g.setColor(Color.white);
			g.drawString(getName() + " v." + getVersion() + " by Hellfish", 10,
					40);
			g.drawString("Running for: " + hours + "h " + minutes + "m "
					+ seconds + "s", 10, 60);
			g.drawString(
					"XP Gained: " + exp + "   Lvls Gained: " + LevelChange, 10,
					80);
			g.drawString("XP To next lvl: " + skills.getXPToNextLevel(type),
					10, 100);
			g.drawString("Progress to next lvl:", 10, 120);
			g.setColor(RED);
			g.fill3DRect(10, 130, 100, 10, true);
			g.setColor(GREEN);
			g.fill3DRect(10, 130, perNextLvl, 10, true); // This is how progress
			// bars are made
			g.setColor(BLACK);
			g.drawString(perNextLvl + "%  to " + nextLvl, 40, 139);
			g.setColor(Color.white);
			g.drawString("Lizards Caught: " + Lizards / 2, 10, 160);

		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) { // Loads any
		// commands
		// needed when the
		// script is started
		return true;
	}

	public void Release() {
		boolean release = false;
		for (int i = 0; i < 27; i++) {
			for (final int element : RedSalamanderCatcher.ReleaseSalamander) {
				if (element == getInventoryItems()[i].getID()) {
					release = true;

				}
			}
			if (release) {
				atInventoryItem(getInventoryItems()[i].getID(),
						"Release Red Sala");
				wait(random(10, 20));
				if (!inventoryContains(10147)) {
					release = false;
				}
			}
		}
	}

	public void serverMessageRecieved(final ServerMessageEvent e) {
		final String word = e.getMessage().toLowerCase();
		if (word.contains("darts away")) {
			Lizards++;
		}
	}
}